package com.java.servlet.web.commons;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@WebFilter(filterName = "SessionFilter", urlPatterns = "/filter/sessionFilter" )
public class SessionFilter implements Filter {
	public static final String LOGIN_PATH = "/login/loginController?method=0";

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("--------------init---------------------");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("--------------doFilter---------------------");
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		//HttpSession isUser = req.isRequestedSessionIdValid();
		//System.out.println("--------------HttpSession isUser  [   "+isUser.getSessionContext().getSession("user")+"---------------------");
		boolean usuario = req.isRequestedSessionIdValid();
		String infoPath = req.getRequestURI();
		//String pathApp = infoPath[1];
		System.out.println("--------------usuario[-_-]  [   "+usuario+"---------------------");
		System.out.println("--------------infoPath  [   "+infoPath+  "---------------------");
		if (usuario) {
			System.out.println("--------------session Activa---------------------");
			chain.doFilter(req,res);
		} else {
			System.out.println("-------------session Inactiva----------------------");
			chain.doFilter(req,res);
		//	res.sendRedirect(infoPath+"login/loginController?method=0");
		}
//				if (isUnusualCondition(req)) {
//				res.sendRedirect("/login/loginController?method=0");
//				} else {
//				chain.doFilter(req,res);
//				}
    }

	@Override
	public void destroy() {
		System.out.println("--------------destroy---------------------");
	}
}
